package com.itheima.youdianda.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.youdianda.entity.SysUser;
import com.itheima.youdianda.entity.YddUser;
import com.itheima.youdianda.mapper.YddUserMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.youdianda.service.YddUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2022-05-24
 */
@Service
public class YddUserServiceImpl extends ServiceImpl<YddUserMapper, YddUser> implements YddUserService {

    @Override
    public Map<String, Object> listByPage(String username, String sex, String mobile, String page, String limit) {
        Page<YddUser> userPage = new Page<>(Integer.parseInt(page), Integer.parseInt(limit));
        LambdaQueryWrapper<YddUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotEmpty(sex),YddUser::getSex,sex)
                .like(StringUtils.isNotEmpty(username),YddUser::getUsername,username)
                .likeRight(StringUtils.isNotEmpty(mobile),YddUser::getMobile,mobile)
                .orderByDesc(YddUser::getId);
        // 根据条件分页查询
        this.page(userPage,wrapper);
        Map<String,Object> map = new HashMap<>();
        map.put("count",Integer.parseInt(String.valueOf(userPage.getTotal())));
        map.put("currentPage",Integer.parseInt(String.valueOf(userPage.getCurrent())));
        map.put("data",userPage.getRecords());
        map.put("pageSize",Integer.parseInt(String.valueOf(userPage.getSize())));
        map.put("totalPages",Integer.parseInt(String.valueOf(userPage.getPages())));
        return map;
    }

    @Override
    public List<Map<String, Object>> getNewUser() {
        IPage<YddUser> userIPage = new Page<>(1,7);
        LambdaQueryWrapper<YddUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(YddUser::getId);
        this.page(userIPage,wrapper);
        List<YddUser> records = userIPage.getRecords();
        List<Map<String,Object>> userList = new ArrayList<>();
        for (YddUser record : records) {
            Map<String,Object> map = new HashMap<>();
            map.put("id",record.getId());
            map.put("username",record.getUsername());
            map.put("sex",record.getSex());
            map.put("icon",record.getIcon());
            map.put("address",record.getAddress());
            userList.add(map);
        }
        return userList;
    }

    @Override
    public Map<String, Object> getUserInfo() {
        Map<String, Object> map = new HashMap<>();
        map.put("total",this.count());
        LambdaQueryWrapper<YddUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(YddUser::getSex,0);
        map.put("user_man",this.count(wrapper));
        LambdaQueryWrapper<YddUser> wrapper1 = new LambdaQueryWrapper<>();
        wrapper.eq(YddUser::getSex,1);
        map.put("user_woman",this.count(wrapper1));
        return map;
    }

}
